package com.soul.dubbo.api.mongo;

import com.soul.model.mongo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.core.query.UpdateDefinition;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author mdoya
 * @version 1.0
 * @description:
 */
@Slf4j
@DubboService
public class SoulApiImpl implements SoulApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询问卷列表
     *
     * @return List<Soul>
     */
    @Override
    public List<Soul> findSoul() {
        return mongoTemplate.findAll(Soul.class);
    }

    @Override
    public void addSoul(Soul soul) {
        mongoTemplate.insert(soul);
    }

    /**
     *  新增问卷
     * @param questions 问卷对象
     * @return 报告id
     */
    @Override
    public String addQuestions(Questions questions) {
        return String.valueOf(mongoTemplate.insert(questions).getId()); // 返回报告id
    }

    /**
     * 新增选项题目
     *
     * @param options
     * @return
     */
    @Override
    public String addOptions(Options options) {
        mongoTemplate.insert(options).getId();
        return null;
    }

    /**
     * 查询灵魂测试结果
     *
     * @param currendUserId 当前用户id
     * @param reportId      报告id
     * @return List<Questions>
     */
    @Override
    public List<Questions> findSoulSubject(Long currendUserId, String reportId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(currendUserId).and("id").is(reportId));
        return mongoTemplate.find(query,Questions.class);
    }

    /**
     * 查询选项编号 选项题目
     *
     * @param optionIds
     * @return
     */
    @Override
    public Map<String, Options> findOptionIds(List<ObjectId> optionIds) {
        // 1. questionIds集合不为空，则questionIds作为条件查询
        Query query = new Query();
        if (!CollectionUtils.isEmpty(optionIds)){
            query.addCriteria(Criteria.where("id").in(optionIds));
        }
        // 2. questionIds集合有值，则查询全部
        List<Options> questionsList = mongoTemplate.find(query, Options.class);
        questionsList.stream().collect(Collectors.toMap(Options::getId, options -> options));
        return null;
    }

    /**
     * 查询灵魂测试报告
     *
     * @param reportId 最新报告id
     * @return
     */
    @Override
    public Report findReport(String reportId) {
        return mongoTemplate.findById(new ObjectId(reportId),Report.class);
    }

    /**
     * 查询当前用户最新报告
     *
     * @param userId
     * @return
     */
    @Override
    public Report findByUserReport(Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        Report r = mongoTemplate.findOne(query, Report.class);
        return r;
    }

    /**
     * 查询QuestionCover集合所有封面
     *
     * @return List<QuestionCover>
     */
    @Override
    public List<QuestionCover> findQuestionCover() {
        return mongoTemplate.findAll(QuestionCover.class);
    }

    /**
     * questionLevelList作为条件查询Questions集合所有题目
     *
     * @param questionLevelList 条件查询
     * @return
     */
    @Override
    public List<Questions> findQuestions(List<String> questionLevelList) {
        Query query = new Query();
        query.addCriteria(Criteria.where("level").in(questionLevelList));
        return mongoTemplate.find(query, Questions.class);
    }

    /**
     * questionsIds作为调条件查询Options集合所有选项题
     *
     * @param questionsIds
     * @return
     */
    @Override
    public List<Options> findOptions(List<String> questionsIds) {
        Query query = new Query();
        query.addCriteria(Criteria.where("questionsId").in(questionsIds));
        return mongoTemplate.find(query,Options.class);
    }

    @Override
    public String addQuestionCover(QuestionCover questionCover) {
        return mongoTemplate.insert(questionCover).getId().toHexString();
    }

    @Override
    public List<Questions> findQuestionsOne(String level) {
        Query query = new Query();
        query.addCriteria(Criteria.where("level").is(level));
        return mongoTemplate.find(query,Questions.class);
    }

    @Override
    public List<Options> findOptionsOne(String questionsId) {
        System.out.println("questionsId: " +questionsId);
        Query query = new Query();
        query.addCriteria(Criteria.where("questionsId").is(questionsId));
        List<Options> optionsList = mongoTemplate.find(query, Options.class);
        System.err.println("optionsList = " + optionsList);
        return optionsList;
    }

    /**
     * 查询Options集合对象
     *
     * @param optionId
     * @return
     */
    @Override
    public Options findQuestionById(String optionId) {
        return mongoTemplate.findById(new ObjectId(optionId),Options.class);
    }

    /**
     * 灵魂测试报告
     *
     * @param report
     */
    @Override
    public void addReport(Report report) {
        mongoTemplate.insert(report);
    }

    /**
     * id查询最新报告
     *
     * @param reportId
     * @return
     */
    @Override
    public Report findReportById(String reportId) {
        return mongoTemplate.findById(new ObjectId(reportId),Report.class);
    }

    /**
     * 查询最新是否存在
     *
     * @param currendUserId
     * @param level
     * @return
     */
    @Override
    public Report findReportExits(Long currendUserId, String level) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(currendUserId).and("level").is(level));
        Report report = mongoTemplate.findOne(query, Report.class);
        return report;
    }

    /**
     * 查询题目级别
     *
     * @param questionId
     * @return
     */
    @Override
    public Questions findQuestionLevel(String questionId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(new ObjectId(questionId)));
        return mongoTemplate.findOne(query,Questions.class);
    }

    /**
     * 查询选项题累加分数
     *
     * @param optionIds
     * @return
     */
    @Override
    public Integer findQuestionByIds(List<ObjectId> optionIds) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").in(optionIds));
        List<Options> optionsList = mongoTemplate.find(query, Options.class);
        Integer score = 0;
        for (Options options : optionsList) {
            score += options.getScore();
        }
        return score;
    }

    /**
     * 修改最新报告
     *
     * @param report
     */
    @Override
    public void updateReport(Report report) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(report.getId()));
        Update update = new Update();
        update.set("conclusion",report.getConclusion());
        update.set("cover",report.getCover());
        update.set("scope",report.getScope());
        mongoTemplate.updateFirst(query,update,Report.class);
    }

    /**
     * 查询当前用户等级最新报告
     *
     * @param userId
     * @param level
     * @return
     */
    @Override
    public Report findReportList(Long userId, String level) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId).and("level").is(level));
        return mongoTemplate.findOne(query,Report.class);
    }
}
